﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DoAn_BumTruong_DesignPattern
{
    class ModuleController : Abstract
    {
        public List<Modules> SelectAll()
        {
            using (var db = new DBContextEntity())
            {
                var query = from md in db.Modules
                            select md;
                return query.ToList();
            }
        }
        //paging
        public List<Modules> Paging(string page, out int howManyPage)
        {
            int pageSize = GlobalConfiguration.PageSize;
            howManyPage = (int)Math.Ceiling((double)SelectAll().Count / (double)pageSize);
            return SelectAll().Skip(pageSize * Convert.ToInt32(page) - pageSize).Take(pageSize).ToList();
        }
        //Single
        public Modules SingleModule(string moduleID)
        {
            int modID = Convert.ToInt32(moduleID);
            using (var db = new DBContextEntity())
            {
                return db.Modules.SingleOrDefault(md => md.ModuleID == modID);
            }
        }

        //
        public override bool Add(object o)
        {
            Modules m = o as Modules;
            using (var db = new DBContextEntity())
            {
                db.Modules.Add(m);
                db.SaveChanges();
            }
            return true;
        }

        //
        public override bool Delete(object o)
        {
            Modules m = o as Modules;
            using (var db = new DBContextEntity())
            {
                var module = db.Modules.Find(m.ModuleID);
                if (module != null)
                {
                    db.Modules.Remove(module);
                    db.SaveChanges();
                }
                return true;
            }
        }

        //
        public override bool Update(object o)
        {
            Modules md = o as Modules;
            using (var db = new DBContextEntity())
            {
                var module = db.Modules.SingleOrDefault(m => m.ModuleID == md.ModuleID);
                if (module != null)
                {
                    module.ModuleID = md.ModuleID;
                    module.Name = md.Name;
                    module.Path = md.Path;
                    module.OrderNo = md.OrderNo;
                    module.PositionDisplay = md.PositionDisplay;
                    module.Active = md.Active;
                    module.Description = md.Description;

                    db.SaveChanges();
                }
                return true;
            }
        }

        //Update Active
        public bool UpdateActive(int id, bool state)
        {
            using (var db = new DBContextEntity())
            {
                var module = db.Modules.Find(id);
                if (module != null)
                {
                    module.ModuleID = id;
                    module.Active = state;
                    db.SaveChanges();
                }
                return true;
            }
        }

        //Update Position
        public bool UpdatePosition(int id, int position)
        {
            using (var db = new DBContextEntity())
            {
                var module = db.Modules.Find(id);
                if (module != null)
                {
                    module.ModuleID = id;
                    module.PositionDisplay = position;
                    db.SaveChanges();
                }
                return true;
            }
        }

        //Update OrderNo
        public bool UpdateOrderNo(int id, int orderNo)
        {
            using (var db = new DBContextEntity())
            {
                var module = db.Modules.Find(id);
                if (module != null)
                {
                    module.ModuleID = id;
                    module.OrderNo = orderNo;
                    db.SaveChanges();
                }
                return true;
            }
        }
    }
}